<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- 输入框 -->
        <fieldset>
            <legend>
                新增一个人
            </legend>
            <div class="form-group">
                <label>姓名：</label>
                <input type="text" v-model="newPerson.name">
            </div>
            <div class="form-group">
                <label>年龄：</label>
                <input type="text" v-model="newPerson.age">
            </div>
            <div class="form-group">
                <label>性别：</label>
                <select v-model="newPerson.sex">
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
            <div class="form-group">
                <label></label>
                <button @click="_add">创建</button>
            </div>
        </fieldset>
        <!-- 列表 -->
        <table>
            <thead>
                <tr>
                    <td>姓名</td>
                    <td>年龄</td>
                    <td>性别</td>
                    <td>删除</td>
                </tr>
            </thead>

            <tbody>
                <tr v-for="person in people">
                    <td>{{person.name}}</td>
                    <td>{{person.age}}</td>
                    <td>{{person.sex}}</td>
                    <td v-bind:class="'text-center'"><button v-on:click="_delete($index)">删除</button></td>

                </tr>
            </tbody>
        </table>
        <button v-on:click="_clear">清除所有</button>
    </div>
    
</body>
<!-- 引入vue -->
<script src="../../vuejs/vue.js"></script>
<script>
    var vm = new Vue({
        el: "#app",

        data: {
            newPerson: {
                name: '',
                age: 0,
                sex: ''
            },
            people: [{
                name: 'Jack',
                age: 30,
                sex: 'Male'
            }, {
                name: 'Bill',
                age: 26,
                sex: 'Male'
            }, {
                name: 'Tracy',
                age: 22,
                sex: 'Female'
            }, {
                name: 'Chris',
                age: 36,
                sex: 'Male'
            }]
        },
        methods: {
            _delete: function (index) {
                //console.info(index);
                //从index 开始删除，删除1条
                this.people.splice(index, 1);
            },
            _add: function () {
                this.people.push(this.newPerson);
                this.newPerson = {
                    name: '',
                    age: 0,
                    sex: '男'
                }
            },
            _clear: function () {
                this.people = [];
            }
        }
    })
</script>

</html>